package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.AdminReceiptAddressRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.AdminReceiptAddressMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.AdminReceiptAddress;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IAdminReceiptAddressService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 【请填写功能名称】业务层处理
 *
 * @author mars
 * @date 2024-07-18
 */
@Slf4j
@Service
@AllArgsConstructor
public class AdminReceiptAddressServiceImpl implements IAdminReceiptAddressService {

    private final AdminReceiptAddressMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdminReceiptAddress add(AdminReceiptAddressRequest request) {
        AdminReceiptAddress entity = AdminReceiptAddress.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdminReceiptAddressRequest request) {
        AdminReceiptAddress entity = AdminReceiptAddress.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public AdminReceiptAddress getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminReceiptAddress> pageList(AdminReceiptAddressRequest request) {
        Page<AdminReceiptAddress> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<AdminReceiptAddress> query = this.buildWrapper(request);
        IPage<AdminReceiptAddress> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<AdminReceiptAddress> list(AdminReceiptAddressRequest request) {
        LambdaQueryWrapper<AdminReceiptAddress> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<AdminReceiptAddress> buildWrapper(AdminReceiptAddressRequest param) {
        LambdaQueryWrapper<AdminReceiptAddress> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getUserId())){
               query.like(AdminReceiptAddress::getUserId ,param.getUserId());
        }
         if (StringUtils.isNotBlank(param.getName())){
               query.like(AdminReceiptAddress::getName ,param.getName());
        }
         if (StringUtils.isNotBlank(param.getPhone())){
               query.like(AdminReceiptAddress::getPhone ,param.getPhone());
        }
         if (StringUtils.isNotBlank(param.getPostCode())){
               query.like(AdminReceiptAddress::getPostCode ,param.getPostCode());
        }
         if (StringUtils.isNotBlank(param.getProvince())){
               query.like(AdminReceiptAddress::getProvince ,param.getProvince());
        }
         if (StringUtils.isNotBlank(param.getCity())){
               query.like(AdminReceiptAddress::getCity ,param.getCity());
        }
         if (StringUtils.isNotBlank(param.getRegion())){
               query.like(AdminReceiptAddress::getRegion ,param.getRegion());
        }
         if (StringUtils.isNotBlank(param.getDetailAddress())){
               query.like(AdminReceiptAddress::getDetailAddress ,param.getDetailAddress());
        }
         if (StringUtils.isNotBlank(param.getCreateUserId())){
               query.like(AdminReceiptAddress::getCreateUserId ,param.getCreateUserId());
        }
         if (StringUtils.isNotBlank(param.getUpdateUserId())){
               query.like(AdminReceiptAddress::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
